<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="试卷名称" align="center" prop="name" />
      <el-table-column label="试题总数" align="center" prop="questionNum" />
      <el-table-column label="是否限时" align="center" width="150">
        <template slot-scope="{ row }">
          <span>{{ row.isTimeLimit === 2 ? "限时" : "不限时" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" width="240">
        <template slot-scope="{ row }">
          <el-button size="mini" type="primary" plain @click="view(row)">
            查看答题人
          </el-button>
          <el-button size="mini" plain @click="view(row, true)">
            查看答题情况
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px" />
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="pageNum"
      :page-size="pageSize"
      :total="total"
      @size-change="onPageSizeChange"
      @current-change="onCurrentPageChange"
    />

    <el-drawer
      size="700px"
      :title="showRank ? '答题情况' : '答题人列表'"
      :visible.sync="drawer.visible"
    >
      <div style="padding: 0 20px">
        <el-table
          v-loading="drawer.listLoading"
          :data="drawer.tableData"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
        >
          <el-table-column
            :label="showRank ? '排名' : '序号'"
            type="index"
            align="center"
            width="60"
            :index="indexMethod"
          />
          <el-table-column label="答题人姓名" align="center" prop="userName" />
          <el-table-column
            v-if="!showRank"
            label="试卷状态"
            align="center"
            prop="answerStatusTxt"
          />
          <el-table-column label="分数" align="center" prop="userScore" />
          <el-table-column
            label="交卷时间"
            align="center"
            prop="answerSubmitTime"
            width="170"
          />
          <el-table-column
            v-if="!showRank"
            label="操作"
            align="center"
            width="100"
          >
            <template slot-scope="{ row }">
              <el-button
                v-if="row.answerStatus === 1"
                size="mini"
                type="primary"
                plain
                @click="marking(row)"
              >
                去阅卷
              </el-button>
              <el-button v-else size="mini" plain @click="markingDetail(row)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="padding: 10px" />
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[10, 20, 50, 100]"
          :current-page="drawer.pageNum"
          :page-size="drawer.pageSize"
          :total="drawer.total"
          @size-change="onDrawerPageSizeChange"
          @current-change="onDrawerCurrentPageChange"
        />
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  getExamPaperPage,
  examPaperOrderByScore,
  modifyExamPaperPage,
} from "@/api2/marking";

export default {
  name: "markingList",
  data() {
    return {
      tableData: [],
      listLoading: true,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      drawer: {
        visible: false,
        tableData: [],
        listLoading: true,
        pageNum: 1,
        pageSize: 10,
        total: 0,
        examPaperId: "",
      },
      showRank: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;
      getExamPaperPage({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }).then((data) => {
        this.listLoading = false;
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    onPageSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getList();
    },
    onCurrentPageChange(val) {
      this.pageNum = val;
      this.getList();
    },
    indexMethod(index) {
      return index + 1 + this.drawer.pageSize * (this.drawer.pageNum - 1);
    },
    async view({ id }, isRank) {
      this.showRank = isRank;
      this.drawer.visible = true;
      this.drawer.examPaperId = id;
      this.drawer.pageNum = 1;
      this.getDrawerList();
    },
    async getDrawerList() {
      this.drawer.listLoading = true;
      const params = {
        pageNum: this.drawer.pageNum,
        pageSize: this.drawer.pageSize,
        examPaperId: this.drawer.examPaperId,
      };
      const fetch = this.showRank ? examPaperOrderByScore : modifyExamPaperPage;
      const res = await fetch(params);
      this.drawer.tableData = res.data.map((item) => {
        item.answerStatusTxt = item.answerStatus == 1 ? "未批阅" : "已批阅";
        return item;
      });
      this.drawer.total = res.total;
      this.drawer.listLoading = false;
    },
    onDrawerPageSizeChange(val) {
      this.drawer.pageSize = val;
      this.drawer.pageNum = 1;
      this.getDrawerList();
    },
    onDrawerCurrentPageChange(val) {
      this.drawer.pageNum = val;
      this.getDrawerList();
    },
    marking(row) {
      const {
        answerId,
        userName: answerPerson,
        answerPerson: answerPersonId,
        examPaperId,
      } = row;
      const obj = { answerPerson, answerPersonId, examPaperId };
      this.drawer.visible = false;
      this.$emit("change", {
        componentName: "markingDetail",
        answerId,
        obj,
      });
    },
    markingDetail({ answerId }) {
      this.drawer.visible = false;
      const obj = {};
      this.$emit("change", { componentName: "markingDetail", answerId, obj });
    },
  },
};
</script>
